package com.chain.qs.modules.biz.service;

import com.chain.qs.common.service.CrudService;
import com.chain.qs.modules.biz.dao.EquipmentGroupItemDao;
import com.chain.qs.modules.biz.entity.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
//@Transactional(readOnly = true)
public class EquipmentGroupItemService extends CrudService<EquipmentGroupItemDao, EquipmentGroupItem> {
    @Resource
    private EquSwitcherService equSwitcherService;
    @Resource
    private EquipmentItemWarningRuleService equipmentItemWarningRuleService;
    @Resource
    private StationGroupCfgExtendService stationGroupCfgExtendService;
    @Resource
    private WarningRecordService warningRecordService;
    @Resource
    private StationGroupService stationGroupService;

    public int getOneByName(EquipmentGroupItem item) {
        return dao.getOneByName(item);
    }

    List<EquipmentGroupItem> findListByEquId(String equipTypeId) {
        return dao.findListByEquId(equipTypeId);
    }

    List<EquipmentGroupItem> findListByGroupId(String groupId) {
        return dao.findListByGroupId(groupId);
    }

    @Transactional(readOnly = false)
    public void delete(EquipmentGroupItem equipmentGroupItem) {
        for (StationGroupCfgExtend stationGroupCfgExtend : stationGroupCfgExtendService.equlistByEquItemId(equipmentGroupItem.getId())) {
            stationGroupCfgExtendService.delete(stationGroupCfgExtend);
        }
        for (WarningRecord warningRecord : warningRecordService.findList(new WarningRecord(null, equipmentGroupItem.getId(), null, null))) {
            warningRecordService.delete(warningRecord);
        }
        for (EquipmentItemWarningRule equipmentItemWarningRule : equipmentItemWarningRuleService.findList(new EquipmentItemWarningRule(null, equipmentGroupItem.getId()))) {
            equipmentItemWarningRuleService.delete(equipmentItemWarningRule);
        }
        for (EquSwitcher equSwitcher : equSwitcherService.findList(new EquSwitcher(null, equipmentGroupItem.getId()))) {
            equSwitcherService.delete(equSwitcher);
        }
//        stationGroupService.deleteRecords(null, equipmentGroupItem.getId());
        super.delete(equipmentGroupItem);
    }
}
